1.2. Using a Data Reader
As you become more and more
familiar with SQL Azure, you'll find that you don't need to make a lot
of changes to your application code except possibly any inline T-SQL.
The beauty of all this is that you're using a proven and trusted
data-access technology, ADO.NET. Thus, nothing really changes. Let's
modify the application and click event code to illustrate this. Follow
these steps:
Add a new list box to the form.
In the click event, add the code in bold in the following snippet. This new code uses the SqlDataReader class to execute a simple SELECT command against the SQL Azure database and then iterate over the SqlDataReader to populate the list box:
private void button1_Click(object sender, EventArgs e)
{
string connStr = GetConString();
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand("SELECT Name FROM Users", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
try
{
while (rdr.Read())
{
listBox1.Items.Add(rdr[0].ToString());
}
rdr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
Run
the application, and click the button on the form. Within a few
seconds, the list box populates with names from the Users table.
The key is that you can
replace the connection string with your local connection string, and it
still works. This is because you're using ADO.NET to handle the
connection, and it doesn't care where the database is. Next, let's take
this example one step further and look at how you use datasets.
1.3. Using a Dataset
In the last example, you found that there is no difference in using a SqlDataReader when querying a SQL Azure database. This example uses the SqlCommand class and the SqlDataAdapter to query SQL Azure and populate a dataset. Here are the steps:
In the button's click event, replace the existing code with the following:
using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandText = "SELECT Name FROM Users";
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataSet ds = new DataSet("Users");
da.Fill(ds);
listBox1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Name";
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
This code creates a new connection using the same connection information as the previous example, and then creates a new SqlCommand instance. The connection, text, and type of the SqlCommand are set and then executed using the instantiated SqlDataAdapter. A new dataset is created and filled from the SqlDataAdapter, which is then applied to the datasource property of the list box.
Run
the application, and click the button on the form. Again, the list box
is populated with the names from the Users table in the SQL Azure
database. Again, you could change the connection string to point to your
local database and the code would work fine.
So, when would code like this not work? Suppose your application had code such as the following, which creates a table without a clustered index:
using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandText = "CREATE TABLE TestTable(ID int, Name varchar(20))";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO TestTable (ID, Name)
VALUES (1, 'Scott'), (2, 'Herve')";
int val = cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Although this code is
certainly valid and runs successfully against your local SQL Server
instance, it doesn't work when executing against your SQL Azure
database. Why? Go ahead and replace the code in the button's click event
with this code, and run the application. The error you get in the
message box states that SQL Azure tables without a clustered index
aren't supported. If you step through the code, you find out that the
table is indeed created, but the error comes from trying to insert data
into the table. You need to go through your application and look for
these sorts of things, to ensure that the application will run
successfully against SQL Azure.
We have discussed
connecting with ADO.NET and the different options we have with ADO.NET,
so let's move on to the other connection option, ODBC.